package com.zyj.myapplication.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;


import com.zyj.myapplication.Activity.ShaituInfoActivity;
import com.zyj.myapplication.Activity.UserTieziActivity;
import com.zyj.myapplication.Javabean.UserShaitu;
import com.zyj.myapplication.Model.Constant;
import com.zyj.myapplication.R;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by owner on 2017/10/12.
 */

public class TieziListAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List mDataList;//数据存储
    private LayoutInflater mLayoutInflater;//准备布局
    private String user_name, user_photo;

    public TieziListAdapter(Context mContext, List mDataList, String user_name) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        this.user_name = user_name;
        mLayoutInflater = LayoutInflater.from(mContext);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(mContext).inflate(R.layout.cardview_tiezilist, parent, false);
        return new TieziListAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final UserShaitu entity = (UserShaitu) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.user_shaitu_name.setText(user_name);
        viewHolder.user_shaitu_time.setText(entity.getUpdate_time());
        viewHolder.user_shaitu_title.setText(entity.getTitle());
        viewHolder.user_shaitu_pic.setImageResource(R.drawable.tx);
        if (entity.getThumb0().equals("")) {
            ((ViewHolder) viewHolder).user_shaitu_photo1.setImageResource(R.drawable.lunbo1);
        } else {
            Picasso.with(mContext).load(Constant.PIC_URL + entity.getThumb0()).into(viewHolder.user_shaitu_photo1);
        }
        if (entity.getThumb1().equals("")) {
            ((ViewHolder) viewHolder).user_shaitu_photo2.setImageResource(R.drawable.lunbo2);
        } else {
            Picasso.with(mContext).load(Constant.PIC_URL + entity.getThumb1()).into(viewHolder.user_shaitu_photo2);
        }
        if (entity.getThumb2().equals("")) {
            ((ViewHolder) viewHolder).user_shaitu_photo3.setImageResource(R.drawable.lunbo3);
        } else {
            Picasso.with(mContext).load(Constant.PIC_URL + entity.getThumb2()).into(viewHolder.user_shaitu_photo3);
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ShaituInfoActivity.class);
                intent.putExtra("shaituid",entity.getId());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView user_shaitu_pic, user_shaitu_photo1, user_shaitu_photo2, user_shaitu_photo3;
        TextView user_shaitu_name, user_shaitu_time, user_shaitu_title;

        public ViewHolder(View itemView) {
            super(itemView);
            user_shaitu_pic = (ImageView) itemView.findViewById(R.id.user_shaitu_pic);
            user_shaitu_photo1 = (ImageView) itemView.findViewById(R.id.user_shaitu_photo1);
            user_shaitu_photo2 = (ImageView) itemView.findViewById(R.id.user_shaitu_photo2);
            user_shaitu_photo3 = (ImageView) itemView.findViewById(R.id.user_shaitu_photo3);
            user_shaitu_name = (TextView) itemView.findViewById(R.id.user_shaitu_name);
            user_shaitu_time = (TextView) itemView.findViewById(R.id.user_shaitu_time);
            user_shaitu_title = (TextView) itemView.findViewById(R.id.user_shaitu_title);
        }
    }
}
